<?php
$test='function "test"(a,   b  ,  c) {}';

function js_skip($text,&$i,$delim='//',$quotes=TRUE,$skip='') {
  $stack = '';
  $test = $skip === '';
  for (;$text{$i}&&(!preg_match($delim,$text{$i}));$i++)     
    if ($text{$i}==='\\') {	  
	  $i++;
	  switch ($text{$i}) {
	    case 'n':
		  if ($test||!preg_match($skip,"\n"))
	        $stack .= "\n";
		  break;
		case 'r':
		  if ($test||!preg_match($skip,"\r"))
		    $stack .= "\r";
		  break;
		case 't':
		  if ($test||!preg_match($skip,"\t"))
		    $stack .= "\t";
		  break;
		default:
		  if ($test||!preg_match($skip,$text{$i}))
		    $stack .= $text{$i};
		  break;
	  }
	} else
	if ($quotes) {
	  
      if ($text{$i}==='\'') 
	    $stack .= '\''.js_skip($text,++$i,'/\'/',FALSE).'\'';
	  else
	  if ($text{$i}==='"') 
	    $stack .= '"'.js_skip($text,++$i,'/"/',FALSE).'"';
	  else if ($test || !preg_match($skip,$text{$i})) $stack .= $text{$i};
	} else if ($test || !preg_match($skip,$text{$i})) $stack .= $text{$i};
  
  
  return $stack;
} // Skips all symbols in string but not delimiter

function js_SyntaxTree($text, &$i, $end='//') {
  $stack = '';
  
  $curobj = array();/*
    Type = {
	  0: Null
	  1: Function
	  2: Array
	  3: Int
	  4: Double
	  5: String
	}
  */
  
  while ($stack = js_skip($text,$i,'/[\s\(\)\[\];\{\}]/')) {//Get next part of string, using  [spaces,brackets,semicolon] as delimiter
    
    $i++;
	
    switch ($stack) {
      case 'function':// New function declaration
	    
	    $curobj['type'] = 1;
        
		$curobj['name'] = js_skip($text,$i,'/\(/');
		
		$i++;
		
		$curobj['args'] = js_skip($text,$i,'/\)/',TRUE,'/\s/');
		print_r($curobj);
		js_skip($text,$i,'/\{/');
		
		$curobj['code'] = js_syntaxTree($text,$i,'/\}/');
				
		$stack = '';
		
        break;
      case 'var': //Local variable definition
	    
		$n = &$curobj['vars'][];
		
		$n['name'] = js_skip($text,$i,'/[\,\=]/')
		
		$i++;
		
		$n['val'] = js_syntaxTree($text,$i,'/[\,\;\}]/');
		
        break;
	  default: // Found variable or function call
	    break;
	}
       
  }
}
$i=0;
js_SyntaxTree($test,$i);
?>
